package com.ruijie.autotest.log.impl.parse;

import com.minelx.landisk.filesystem.core.File;
import com.ruijie.autotest.log.impl.data.Columns;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.io.SAXReader;
import org.dom4j.tree.DefaultElement;

import java.util.List;

public class XmlColumnsParser implements IColumnsParser {
	static final String XPATH_ALL_COLUMN_ITEM = "//DATASET[@name='WA_COMMON_010015']//ITEM";

	@Override
	public Columns parseColumns(File columnFile) {
		try {
			Document document = new SAXReader(new DocumentFactory()).read(columnFile.getAbsolutePath());
			return new Columns(((List<DefaultElement>) document.getRootElement().selectNodes(XPATH_ALL_COLUMN_ITEM)).stream()
					.map(item -> item.attributeValue("eng"))
					.toArray(String[]::new));
		} catch (DocumentException e) {
			throw new RuntimeException(e);
		}
	}
}
